import requests
import time
from concurrent.futures import ThreadPoolExecutor

# 配置服务器URL和并发线程数
url = "http://192.168.254.130:8089"  # 替换为你的服务器地址
concurrent_requests = 100  # 并发请求数量
total_requests = 1000  # 总请求数量

# 发送HTTP请求的函数
def send_request():
    try:
        response = requests.get(url)
        return response.status_code
    except Exception as e:
        print(f"请求失败: {e}")
        return None

# 主测试函数
def test_qps():
    with ThreadPoolExecutor(max_workers=concurrent_requests) as executor:
        start_time = time.time()
        
        # 提交所有请求
        futures = [executor.submit(send_request) for _ in range(total_requests)]
        
        # 等待所有请求完成
        responses = [f.result() for f in futures]
        end_time = time.time()
        
        # 计算QPS
        duration = end_time - start_time
        successful_requests = sum(1 for r in responses if r == 200)
        qps = successful_requests / duration
        print(f"总请求数: {total_requests}, 成功请求数: {successful_requests}")
        print(f"耗时: {duration:.2f} 秒")
        print(f"QPS: {qps:.2f}")

if __name__ == "__main__":
    test_qps()